package com.ruoyi.zsyh.controller;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.common.utils.EmptyUtils;
import com.ruoyi.common.utils.IdWorker;
import com.ruoyi.zsyh.domain.SysDtArea;
import com.ruoyi.zsyh.domain.ZsyhFactoryBalanceDetails;
import com.ruoyi.zsyh.service.ISysDtAreaService;
import com.ruoyi.zsyh.service.IZsyhFactoryBalanceDetailsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.zsyh.domain.ZsyhPlatformFactory;
import com.ruoyi.zsyh.service.IZsyhPlatformFactoryService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 厂家信息档案Controller
 * 
 * @author yuexl
 * @date 2021-12-08
 */
@Controller
@RequestMapping("/zsyh/factory")
public class ZsyhPlatformFactoryController extends BaseController
{
    private String prefix = "zsyh/factory";

    @Autowired
    private IZsyhPlatformFactoryService zsyhPlatformFactoryService;
    @Autowired
    private ISysDtAreaService sysDtAreaService;
    @Autowired
    private IZsyhFactoryBalanceDetailsService zsyhFactoryBalanceDetailsService;

    @RequiresPermissions("zsyh:factory:view")
    @GetMapping()
    public String factory()
    {
        return prefix + "/factory";
    }

    @RequiresPermissions("zsyh:factory:view")
    @GetMapping("/factoryApplyReview")
    public String factoryApplyReview()
    {
        return prefix + "/factoryApplyReview";
    }

    @RequiresPermissions("zsyh:factory:view")
    @GetMapping("/addBalance/{id}")
    public String addBalance(@PathVariable("id") String id, ModelMap mmap)
    {
        mmap.put("id",id);
        return prefix + "/addBalance";
    }

    @RequiresPermissions("zsyh:factory:view")
    @GetMapping("/addMoney/{id}")
    public String addMoney(@PathVariable("id") String id, ModelMap mmap)
    {
        mmap.put("id",id);
        return prefix + "/addMoney";
    }

    @RequiresPermissions("zsyh:factory:view")
    @GetMapping("/reviewFactory/{id}")
    public String reviewFactory(@PathVariable("id") String id, ModelMap mmap)
    {
        mmap.put("id",id);
        return prefix + "/reviewFactory";
    }

    @RequiresPermissions("zsyh:factory:view")
    @GetMapping("/viewAccount/{id}")
    public String viewAccount(@PathVariable("id") String id, ModelMap mmap)
    {
        mmap.put("factoryId",id);
        return prefix + "/viewAccount";
    }

    @RequiresPermissions("zsyh:factory:view")
    @GetMapping("/selectFactory")
    public String addBalance()
    {
        return prefix + "/selectFactory";
    }

    @RequiresPermissions("zsyh:factory:edit")
    @GetMapping("/resetPwd/{id}")
    public String resetPwd(@PathVariable("id") String id, ModelMap mmap)
    {
        mmap.put("zsyhPlatformFactory", zsyhPlatformFactoryService.selectZsyhPlatformFactoryById(id));
        return prefix + "/resetPwd";
    }

    @RequiresPermissions("zsyh:factory:edit")
    @Log(title = "重置密码", businessType = BusinessType.UPDATE)
    @PostMapping("/resetPwd")
    @ResponseBody
    public AjaxResult resetPwdSave(ZsyhPlatformFactory zsyhPlatformFactory)
    {

        zsyhPlatformFactory.setFactoryPassword(new Md5Hash(zsyhPlatformFactory.getFactoryPassword()).toHex());
        int num = zsyhPlatformFactoryService.updateZsyhPlatformFactory(zsyhPlatformFactory);
        return toAjax(num);
    }

    /**
     * 查询厂家信息档案列表
     */
    @RequiresPermissions("zsyh:factory:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ZsyhPlatformFactory zsyhPlatformFactory)
    {
        startPage();
        List<ZsyhPlatformFactory> list = zsyhPlatformFactoryService.selectZsyhPlatformFactoryList(zsyhPlatformFactory);
        return getDataTable(list);
    }

    /**
     * 获取省
     */
    @GetMapping("/{parentId}")
    @ResponseBody
    public TableDataInfo provinceData(@PathVariable("parentId") String parentId)
    {
        SysDtArea sysDtArea = new SysDtArea();
        sysDtArea.setAreaParentId(parentId);
        List<SysDtArea> proList = sysDtAreaService.selectSysDtAreaList(sysDtArea);
        return getDataTable(proList);
    }

    /**
     * 导出厂家信息档案列表
     */
    @RequiresPermissions("zsyh:factory:export")
    @Log(title = "厂家信息档案", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ZsyhPlatformFactory zsyhPlatformFactory)
    {
        List<ZsyhPlatformFactory> list = zsyhPlatformFactoryService.selectZsyhPlatformFactoryList(zsyhPlatformFactory);
        ExcelUtil<ZsyhPlatformFactory> util = new ExcelUtil<ZsyhPlatformFactory>(ZsyhPlatformFactory.class);
        return util.exportExcel(list, "厂家信息档案数据");
    }

    /**
     * 新增厂家信息档案
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存厂家信息档案
     */
    @RequiresPermissions("zsyh:factory:add")
    @Log(title = "厂家信息档案", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ZsyhPlatformFactory zsyhPlatformFactory)
    {
        zsyhPlatformFactory.setId(IdWorker.getId());
        //默认通过
        zsyhPlatformFactory.setFactoryState("1");
        //加密密码
        zsyhPlatformFactory.setFactoryPassword(new Md5Hash(zsyhPlatformFactory.getFactoryPassword()).toHex());
        return toAjax(zsyhPlatformFactoryService.insertZsyhPlatformFactory(zsyhPlatformFactory));
    }

    /**
     * 校验手机号码
     */
    @PostMapping("/checkPhoneUnique")
    @ResponseBody
    public String checkPhoneUnique(ZsyhPlatformFactory zsyhPlatformFactory)
    {
        //判断是否有重复的手机号
        ZsyhPlatformFactory zsyhPlatformFactory1 = new ZsyhPlatformFactory();
        zsyhPlatformFactory1.setFactoryPhone(zsyhPlatformFactory.getFactoryPhone());
        List<ZsyhPlatformFactory> zsyhPlatformFactoryList = zsyhPlatformFactoryService.selectZsyhPlatformFactoryList(zsyhPlatformFactory1);
        if(EmptyUtils.isNotEmpty(zsyhPlatformFactory.getId())){
            if(zsyhPlatformFactoryList.size()>1){
                return "1";
            }
        }else{
            if(zsyhPlatformFactoryList.size()>0){
                return "1";
            }
        }
        return "0";
    }

    /**
     * 校验email邮箱
     */
    @PostMapping("/checkEmailUnique")
    @ResponseBody
    public String checkEmailUnique(ZsyhPlatformFactory zsyhPlatformFactory)
    {
        //判断是否有重复的email邮箱
        ZsyhPlatformFactory zsyhPlatformFactory1 = new ZsyhPlatformFactory();
        zsyhPlatformFactory1.setFactoryEmail(zsyhPlatformFactory.getFactoryEmail());
        List<ZsyhPlatformFactory> zsyhPlatformFactoryList = zsyhPlatformFactoryService.selectZsyhPlatformFactoryList(zsyhPlatformFactory1);
        if(EmptyUtils.isNotEmpty(zsyhPlatformFactory.getId())){
            if(zsyhPlatformFactoryList.size()>1){
                return "1";
            }
        }else{
            if(zsyhPlatformFactoryList.size()>0){
                return "1";
            }
        }
        return "0";
    }

    /**
     * 修改厂家信息档案
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        ZsyhPlatformFactory zsyhPlatformFactory = zsyhPlatformFactoryService.selectZsyhPlatformFactoryById(id);
        mmap.put("zsyhPlatformFactory", zsyhPlatformFactory);
        return prefix + "/edit";
    }

    /**
     * 修改厂家信息档案
     */
    @GetMapping("/viewFactory/{id}")
    public String viewFactory(@PathVariable("id") String id, ModelMap mmap)
    {
        ZsyhPlatformFactory zsyhPlatformFactory = zsyhPlatformFactoryService.selectZsyhPlatformFactoryById(id);
        mmap.put("zsyhPlatformFactory", zsyhPlatformFactory);
        return prefix + "/viewFactory";
    }

    /**
     * 修改保存厂家信息档案
     */
    @RequiresPermissions("zsyh:factory:edit")
    @Log(title = "厂家信息档案", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ZsyhPlatformFactory zsyhPlatformFactory)
    {
        return toAjax(zsyhPlatformFactoryService.updateZsyhPlatformFactory(zsyhPlatformFactory));
    }

    /**
     * 删除厂家信息档案
     */
    @RequiresPermissions("zsyh:factory:remove")
    @Log(title = "厂家信息档案", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(zsyhPlatformFactoryService.deleteZsyhPlatformFactoryByIds(ids));
    }

    /**
     * 查看营业执照
     */
    @GetMapping("/avatar/{id}")
    public String avatar(@PathVariable("id")String id,ModelMap mmap)
    {
        mmap.put("zsyhPlatformFactory", zsyhPlatformFactoryService.selectZsyhPlatformFactoryById(id));
        return prefix + "/avatar";
    }

    /**
     * 新增保存厂家信息档案
     */
    @Transactional
    @RequiresPermissions("zsyh:factory:add")
    @PostMapping("/addBalance")
    @ResponseBody
    public AjaxResult addBalance(BigDecimal addBalance,String id,String type)
    {
        ZsyhPlatformFactory zsyhPlatformFactory =zsyhPlatformFactoryService.selectZsyhPlatformFactoryById(id);
        ZsyhPlatformFactory zsyhPlatformFactory1 = new ZsyhPlatformFactory();
        zsyhPlatformFactory1.setId(id);
        //添加余额变动信息
        ZsyhFactoryBalanceDetails zsyhFactoryBalanceDetails = new ZsyhFactoryBalanceDetails();
        zsyhFactoryBalanceDetails.setId(IdWorker.getId());
        zsyhFactoryBalanceDetails.setFactoryType("2");
        zsyhFactoryBalanceDetails.setFactoryMoney(addBalance);
        zsyhFactoryBalanceDetails.setFactoryId(id);
        if("balance".equals(type)){
            zsyhFactoryBalanceDetails.setFactoryClassify("2");
            zsyhFactoryBalanceDetails.setFactoryBalance(zsyhPlatformFactory.getFactoryBalance().add(addBalance));
            zsyhPlatformFactory1.setFactoryBalance(addBalance);
        }else if("money".equals(type)){
            zsyhFactoryBalanceDetails.setFactoryClassify("1");
            zsyhFactoryBalanceDetails.setFactoryBalance(zsyhPlatformFactory.getFactoryMoney().add(addBalance));
            zsyhPlatformFactory1.setFactoryMoney(addBalance);
        }
        int num = zsyhPlatformFactoryService.updateFactoryBalance(zsyhPlatformFactory1);
        if(num >0){
            zsyhFactoryBalanceDetailsService.insertZsyhFactoryBalanceDetails(zsyhFactoryBalanceDetails);
        }
        return new AjaxResult(AjaxResult.Type.SUCCESS,"充值成功",num);
    }

    /**
     * 审核厂家注册
     */
    @Transactional
    @RequiresPermissions("zsyh:factory:add")
    @PostMapping("/reviewFactory")
    @ResponseBody
    public AjaxResult reviewFactory(String rejectionReason,String id,String type)
    {
        ZsyhPlatformFactory zsyhPlatformFactory =new ZsyhPlatformFactory();
        zsyhPlatformFactory.setId(id);
        zsyhPlatformFactory.setFactoryState(type);
        zsyhPlatformFactory.setRejectionReason(rejectionReason);
        return toAjax(zsyhPlatformFactoryService.updateZsyhPlatformFactory(zsyhPlatformFactory));
    }

}
